//
//  BIZDWPlayerViewController.m
//  biz
//
//  Created by zhangfuwei on 2017/3/17.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZDWPlayerViewController.h"
#import "BIZLoginViewController.h"
#import "BIZDWPlayerHomeViewController.h"
#import "BIZRemoteNotificationWebVC.h"
// cell
#import "BIZFeatureCollectionViewCell.h"
#import "BIZFeaturePlayerTableViewCell.h"
#import "BIZTagListCollectionViewCell.h"
#import "BIZNoConsultorCell.h"

// model
#import "BIZMoviePlayCategoryListModel.h"
#import "BIZMoviePlaySitcomListModel.h"
#import "BIZCycleScrollViewImagesModel.h"

// customView
#import <SDCycleScrollView.h>
#import <TTGTagCollectionView.h>

//static CGFloat kStatusH = 20;
static CGFloat kScrollViewH = 150;
static CGFloat kCollectionViewH = 110;
static CGFloat kTagListViewH = 150;
static CGFloat kCollectionViewItemW = 48;
static CGFloat kTagItemH = 30;
static CGFloat kTagItemW = 100;
static CGFloat kTagMargin = 19;

static CGFloat kFeatureCollectionViewTag = 1;
static CGFloat kTagListCollectionViewTag = 2;
static NSString *kCollectionViewCellReuseID = @"kCollectionViewCellReuseID";
static NSString *kTableViewCellReuseID = @"kTableViewCellReuseID";
static NSString *kTagListCellReuseID = @"kTagListCellReuseID";
static NSString *kNoDataCellReuseID = @"kNoDataCellReuseID";

typedef void (^SDScrollViewClickItemOperationBlock)(NSInteger currentIndex);
typedef enum : NSUInteger {
    
    BIZCategoryListTypeTopCate = 0,
    BIZCategoryListTypeSubCateTopic,
    BIZCategoryListTypeSubCateTaxes,
    BIZCategoryListTypeSubCateTrade
    
} BIZCategoryListType;

@interface BIZDWPlayerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

/** 顶部轮播器 */
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
/** 专题collectionView */
@property (nonatomic, strong) UICollectionView *featuresCollectionView;
/** tableView */
@property (nonatomic, strong) UITableView *tableView;

/** 顶级标签列表模型 */
@property (nonatomic, strong) BIZMoviePlayCategoryListModel *topCateListM;

/** 次级标签列表模型 */
@property (nonatomic, strong) BIZMoviePlayCategoryListModel *subCateListM;
/** 次级专题标签列表模型 */
@property (nonatomic, strong) BIZMoviePlayCategoryListModel *subTopicCateListM;
/** 次级税种标签列表模型 */
@property (nonatomic, strong) BIZMoviePlayCategoryListModel *subTaxesCateListM;
/** 次级行业标签列表模型 */
@property (nonatomic, strong) BIZMoviePlayCategoryListModel *subTradeCateListM;

/** 应该刷新标签列表的数据类型type(专题,税种,行业) */
@property (nonatomic, assign) BIZCategoryListType type;

///** 标签列表模型 */
//@property (nonatomic, strong) BIZMoviePlayCategoryListModel *firstCateListM;
///** 标签列表模型 */
//@property (nonatomic, strong) BIZMoviePlayCategoryListModel *secondCateListM;
///** 标签列表模型 */
//@property (nonatomic, strong) BIZMoviePlayCategoryListModel *thirdCateListM;
//

/** 电视剧列表模型 */
@property (nonatomic, strong) BIZMoviePlaySitcomListModel *sitcomListM;

/** tags 数组 */
@property (nonatomic, strong) NSMutableArray *tagsMutableArray;

/** tags 视图 */
@property (nonatomic, strong) UICollectionView *tagListView;

/** arrow */
@property (nonatomic, strong) UIImageView *arrowImgV;


/** maskView */
@property (nonatomic, strong) UIView *maskBlackView;

/** 全局队列组 */
@property (nonatomic, strong) dispatch_group_t globalGroup;

/** 数据源最后的ID */
@property (nonatomic, assign) int lastId;

/** 轮播图列表 */
@property (nonatomic, strong) NSMutableArray *cycleScrollViewImgesList;

/** 轮播图模型列表 */
@property (nonatomic, strong) BIZCycleScrollViewImagesModel *cycleScrollViewImgesModel;

/** 筛选器--子标签模型 */
@property (nonatomic, strong) BIZMoviePlayCategoryListDataModel *filterCateModel;


@end

@implementation BIZDWPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 导航栏标题
    [self setNavBar];
    
    // 数组,模型等值初始化
    [self setupConfig];
    
    // 加载视图
    [self loadUI];

    // 网络请求(按照一定顺序进行网络请求)
//    [self loadRequestByGCDGroup];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.sitcomListM ?:[self.tableView.mj_header beginRefreshing];
    // 请求数据
    [self reloadDataWhenNoData];
}

- (void)setNavBar {
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"在线课堂";
}

- (void)setupConfig {
    
    _lastId = 1;
    self.cycleScrollViewImgesList = [[NSMutableArray alloc] initWithCapacity:3];
    _filterCateModel = nil;
    
}

/** 加载顶部视图 */
- (void)loadUI {
    
    [self.view addSubview:self.cycleScrollView];
    
    [self.view addSubview:self.featuresCollectionView];

    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.featuresCollectionView.mas_bottom);
        make.bottom.equalTo(self.view).offset(-49);
    }];
}

- (void)reloadDataWhenNoData {
    
    self.cycleScrollViewImgesList.count ?: [self requestCycleScrollViewImageURLStringsWithType:@"1"];

    self.topCateListM.data.count ?:[self requestCategoryListForFeaturesWithFatherTag:@"0" cateType:BIZCategoryListTypeTopCate];
}

#pragma mark -- 请求轮播图

- (void)requestCycleScrollViewImageURLStringsWithType:(NSString *)type {
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kCycleScrollViewImageGroupURL
           parameters:@{@"type":type?:@"0"}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  
                  DeLog(@"轮播图数据response：%@", responseObject);
                  
                  BIZCycleScrollViewImagesModel *model = [BIZCycleScrollViewImagesModel yy_modelWithJSON:responseObject];
                  self.cycleScrollViewImgesModel = model;
                  
                  if (model.code.intValue == 1) {
                      [MBProgressHUD hideHUD];
                      [model.data enumerateObjectsUsingBlock:^(BIZCycleScrollViewImagesDataModel *dataM, NSUInteger idx, BOOL * _Nonnull stop) {
                          NSString *urlStr = [NSString stringWithFormat:@"%@/%@/%@", kMainBaseUrl,kShowImage,dataM.image];
                          [self.cycleScrollViewImgesList addObject:urlStr];
                      }];
                      self.cycleScrollView.imageURLStringsGroup = self.cycleScrollViewImgesList;
                      
                  }else if (model.code.intValue == 2) {
                      
                      [MBProgressHUD showErrorMessage:model.des];
                      BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
                      
                      MJWeakSelf;
                      vc.loginSuccessBlock = ^(){
                          [weakSelf requestCycleScrollViewImageURLStringsWithType:@"0"];
                      };
                      [self presentViewController:vc animated:YES completion:nil];
                  }else {
                      
                      [MBProgressHUD showErrorMessage:model.des];
                  }
                  
                  
              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"轮播图数据error：%@", error);
                  
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }
                  
              }];
}

#pragma mark -- 点播类别标签
- (void)requestCategoryListForFeaturesWithFatherTag:(NSString *)tag cateType:(BIZCategoryListType)type {
    
    [RequestTool POST:kMoviePlayCategoryListUrl
           parameters:@{@"pid":tag}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"点播类别标签--%@",responseObject);
                  
                  BIZMoviePlayCategoryListModel *cateListM = [BIZMoviePlayCategoryListModel yy_modelWithJSON:responseObject];
                  
                  if (cateListM.code.intValue == 1) { // 给顶级标签列表赋值
                      
                      [MBProgressHUD showSuccessMessage:@"子标签获取成功"];
                      
                      switch (type) {
                          case BIZCategoryListTypeTopCate:
                              
                              self.topCateListM = cateListM;
                              [self.featuresCollectionView reloadData];
                              break;
                          case BIZCategoryListTypeSubCateTopic:

                              self.subTopicCateListM = cateListM;
                              [self.tagListView reloadData];
                              
                              break;
                          case BIZCategoryListTypeSubCateTaxes:
                              
                              self.subTaxesCateListM = cateListM;
                              [self.tagListView reloadData];

                              break;
                          case BIZCategoryListTypeSubCateTrade:
                              
                              self.subTradeCateListM = cateListM;
                              [self.tagListView reloadData];
                              break;
                              
                          default:
                              break;
                      }
                  }else if (cateListM.code.intValue == 2){ // 未登录
                      
                      [MBProgressHUD showWarnMessage:@"请登录"];
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];

                  }else {
                      
                      DeLog(@"类别标签--%@",cateListM.des);
                      [MBProgressHUD showWarnMessage:@"系统异常" timer:2.5];
                  }
                  if (_globalGroup) {
                      dispatch_group_leave(_globalGroup);
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:@"网络不给力" timer:2.5];
                  DeLog(@"error ---%@",error);
                  
                  if (_globalGroup) {
                      dispatch_group_leave(_globalGroup);
                  }

              }
     ];

}


#pragma mark -- 点播电视剧列表
- (void)requestMoviePlayListWithTrade:(NSString *)trade tax:(NSString *)tax topic:(NSString *)topic pageIndex:(NSNumber *)index pageEnd:(NSNumber *)end {
    
    DeLog(@"%@, %@ ,%@, %@ %@",trade,tax,topic,index,end);
    [MBProgressHUD showActivityMessageInView:nil];
    
    NSDictionary *param1 = @{@"trade":trade?:@"",@"tax":tax?:@"",@"topic":topic?:@"",@"pageIndex":index?:@1,@"pageEnd":end?:@9};
    
    [RequestTool POST:kMoviePlaySitcomListUrl
           parameters:param1
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"点播电视剧列表--%@",responseObject);
                  [self stopRefresh];
                  [MBProgressHUD hideHUD];
                  
                  BIZMoviePlaySitcomListModel *sitcomListM = [BIZMoviePlaySitcomListModel yy_modelWithJSON:responseObject];
                  
                  if (sitcomListM.code.intValue == 1) {
                    
                      if (index.intValue == 1) {
                          self.sitcomListM = sitcomListM;
                      }else{
                          
                          self.sitcomListM.data = [self.sitcomListM.data arrayByAddingObjectsFromArray:sitcomListM.data];
                      }
                      _lastId += (int)sitcomListM.data.count;
                      [self.tableView reloadData];
                      
                  }else if (sitcomListM.code.intValue == 2){ // 未登录
                      
                      [MBProgressHUD showWarnMessage:@"请登录"];
                      [self presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showWarnMessage:@"系统异常" timer:2.5];
                  }
                  if (_globalGroup) {
                      dispatch_group_leave(_globalGroup);
                  }


              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"error ---%@",error);
                  [MBProgressHUD hideHUD];
                  [self stopRefresh];
                  [MBProgressHUD showErrorMessage:@"网络不给力" timer:2.5];
                  if (_globalGroup) {
                      dispatch_group_leave(_globalGroup);
                  }

              }
     ];
}

#pragma mark -- collectionView 数据源,代理

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView.tag == kFeatureCollectionViewTag) {
        return self.topCateListM ? self.topCateListM.data.count+1 : 1;
    }else{
        switch (_type) {
            case BIZCategoryListTypeSubCateTopic:
                return self.subTopicCateListM.data.count;
                break;
            case BIZCategoryListTypeSubCateTaxes:
                return self.subTaxesCateListM.data.count;
                break;
            case BIZCategoryListTypeSubCateTrade:
                return self.subTradeCateListM.data.count;
                break;
            default:
                return 0;
                break;
        }
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == kFeatureCollectionViewTag) {
        
        BIZFeatureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuseID forIndexPath:indexPath];
        BIZMoviePlayCategoryListDataModel *dataM ;
        
        cell.imageName = [NSString stringWithFormat:@"feature_%ld",indexPath.row];
    
        switch (indexPath.row) {
            case 0:
                cell.titleName = @"全部";
                 cell.imageName = [NSString stringWithFormat:@"feature_0"];
                break;
            default:
                dataM = self.topCateListM.data[indexPath.row-1];
                cell.titleName = dataM.name;
                cell.imageName = [NSString stringWithFormat:@"feature_%d",dataM.uuid.intValue];

                break;
        }
        return cell;
    }else { // tagList 的cell
        BIZMoviePlayCategoryListModel *subCateListM;

        switch (_type) {
            case BIZCategoryListTypeSubCateTopic:
                subCateListM = self.subTopicCateListM;
                break;
            case BIZCategoryListTypeSubCateTaxes:
                subCateListM = self.subTaxesCateListM;
                break;
            case BIZCategoryListTypeSubCateTrade:
                subCateListM = self.subTradeCateListM;
                break;
            default:
                break;
        }

        BIZTagListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTagListCellReuseID forIndexPath:indexPath];
        BIZMoviePlayCategoryListDataModel *cateListDataM = subCateListM.data[indexPath.row];
        cell.listDataM = cateListDataM;
//        cell.clickBlock = [self returnTagClickBlockWithModel:cateListDataM];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (collectionView.tag == kTagListCollectionViewTag) { // 子标签点击
        
        BIZMoviePlayCategoryListModel *subCateListM;
        
        switch (_type) {
            case BIZCategoryListTypeSubCateTopic:
                subCateListM = self.subTopicCateListM;
                break;
            case BIZCategoryListTypeSubCateTaxes:
                subCateListM = self.subTaxesCateListM;
                break;
            case BIZCategoryListTypeSubCateTrade:
                subCateListM = self.subTradeCateListM;
                break;
            default:
                break;
        }
        BIZMoviePlayCategoryListDataModel *cateListDataM = subCateListM.data[indexPath.row];
        [self didSelectSubcateCellWithModel:cateListDataM];
        return;
    }else if (!indexPath.row) {// 点击"全部"
        [MBProgressHUD showTipMessageInView:@"已重置筛选"];
        [self loadDataWithNoFilter];
        return;
    }else if (!_topCateListM.data.count) {// 顶级标签数据为空,重新请求
        
        [MBProgressHUD showTipMessageInView:@"正在请求一级标签,再次点击请求子标签"];
        [self loadDataWithNoFilter];
        return;
    }
    
    BIZMoviePlayCategoryListDataModel *topCateM = self.topCateListM.data[indexPath.row-1];
    
    // 如果数据存在就直接刷新,不请求网络
    switch (topCateM.uuid.intValue) {
        case 1: // 行业
            _type = BIZCategoryListTypeSubCateTrade;
            break;
        case 2: // 税种
            _type = BIZCategoryListTypeSubCateTaxes;
            break;
        case 3: // 专题
            _type = BIZCategoryListTypeSubCateTopic;
            break;
        default:
            break;
    }
    
    if (_subTopicCateListM && _subTaxesCateListM && _subTradeCateListM) {
        
        [self.tagListView reloadData];

    }else {
        
        [self.topCateListM.data enumerateObjectsUsingBlock:^(BIZMoviePlayCategoryListDataModel *cateListDataM, NSUInteger idx, BOOL * _Nonnull stop) {
            
            BIZCategoryListType type = BIZCategoryListTypeTopCate;
            switch (cateListDataM.uuid.intValue) {
                case 1:
                    type = BIZCategoryListTypeSubCateTrade;
                    break;
                case 2:
                    type = BIZCategoryListTypeSubCateTaxes;
                    break;
                case 3:
                    type = BIZCategoryListTypeSubCateTopic;
                    break;
                default:
                    break;
            }
            
            [self requestCategoryListForFeaturesWithFatherTag:cateListDataM.uuid cateType:type];
        }];
    }
    
    
    [self.view insertSubview:self.maskBlackView aboveSubview:self.view];
    
    [self.arrowImgV setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tagArrow_%ld",indexPath.row]]];

    [self.view insertSubview:self.arrowImgV aboveSubview:self.maskBlackView];
    
    [self.view insertSubview:self.tagListView aboveSubview:self.maskBlackView];
    
}

- (void)loadDataWithNoFilter {
    _lastId = 1;
    _sitcomListM = nil;
    // 把选择器uuid置为初始值
    _filterCateModel = nil;
    _type = BIZCategoryListTypeTopCate;
    [self requestMoviePlayListWithTrade:nil tax:nil topic:nil pageIndex:@(_lastId) pageEnd:@(_lastId+9)];

}

#pragma mark -- tableView 的数据源,代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.sitcomListM.data.count?:1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.sitcomListM.data.count) {
        
        BIZFeaturePlayerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuseID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //设置 cell 的数据
        
        BIZMoviePlaySitcomListDataModel *dataM = self.sitcomListM.data[indexPath.row];

        cell.sitcomListDataM = dataM;
        cell.row = indexPath.row;
    
        return cell;
    }else {
        
        // MARK:无数据cell
        BIZNoConsultorCell *noDataCell = [tableView dequeueReusableCellWithIdentifier:kNoDataCellReuseID forIndexPath:indexPath];
        noDataCell.errorMsg = @"没有符合要求的电视剧";
        noDataCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return noDataCell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (!self.sitcomListM.data.count) {
        return;
    }
    BIZMoviePlaySitcomListDataModel *dataM = self.sitcomListM.data[indexPath.row];
    BIZDWPlayerHomeViewController *playHomeVC = [[BIZDWPlayerHomeViewController alloc] initWithDataModel:dataM];
    playHomeVC.playMode = NO;
    [self.navigationController pushViewController:playHomeVC animated:YES];

}

#pragma mark -- private methods 

- (SDScrollViewClickItemOperationBlock)scrollViewClickItemBlock {
    SDScrollViewClickItemOperationBlock block ;
    __weak BIZDWPlayerViewController *weakSelf = self;
    block = ^(NSInteger currentIndex) {
        BIZCycleScrollViewImagesDataModel *dataM = weakSelf.cycleScrollViewImgesModel.data[currentIndex];
        DeLog(@"title-%@,content-%@",dataM.title,dataM.content);
        
        if (dataM.isNative.intValue == 0) {// 跳转原生界面
            // 预留接口
            BIZRemoteNotificationWebVC *webVC = [[BIZRemoteNotificationWebVC alloc] initWithWebContentId:dataM.imageId];
            webVC.navigationItem.title = dataM.title;

            [weakSelf.navigationController pushViewController:webVC animated:YES];
            
        }else if (dataM.isNative.intValue == 1){ // 跳转网页
            BIZRemoteNotificationWebVC *webVC = [[BIZRemoteNotificationWebVC alloc] initWithWebContentId:dataM.imageId];
            webVC.navigationItem.title = dataM.title;
            [weakSelf.navigationController pushViewController:webVC animated:YES];
            
        }
    };
    return block;
}


//- (BIZTagListCollectionViewCellClickBlock)returnTagClickBlockWithModel: (BIZMoviePlayCategoryListDataModel *)cateListDataM {
//    
//    BIZTagListCollectionViewCellClickBlock block ;
//    __weak BIZDWPlayerViewController *weakSelf =  self;
//    block = ^{
//        _lastId = 1;
//        weakSelf.sitcomListM = nil;
//        _filterCateModel = cateListDataM;
//        
//        NSString *trade     = @"";
//        NSString *tax       = @"";
//        NSString *topic     = @"";
//        switch (_filterCateModel.pid.intValue) {
//            case 1:
//                trade   = _filterCateModel.uuid;
//                break;
//            case 2:
//                tax     = _filterCateModel.uuid;
//                break;
//            case 3:
//                topic   = _filterCateModel.uuid;
//                break;
//            default:
//                break;
//        }
//        [weakSelf requestMoviePlayListWithTrade:trade
//                                            tax:tax
//                                          topic:topic
//                                      pageIndex:@(_lastId)
//                                        pageEnd:@(_lastId+9)
//         ];
//        [weakSelf removeMaskBlackView];
//    };
//    return block;
//}

- (void)didSelectSubcateCellWithModel:(BIZMoviePlayCategoryListDataModel*) cateDataModel{
    _lastId = 1;
    self.sitcomListM = nil;
    _filterCateModel = cateDataModel;
    
    NSString *trade     = @"";
    NSString *tax       = @"";
    NSString *topic     = @"";
    switch (_filterCateModel.pid.intValue) {
        case 1:
            trade   = _filterCateModel.uuid;
            break;
        case 2:
            tax     = _filterCateModel.uuid;
            break;
        case 3:
            topic   = _filterCateModel.uuid;
            break;
        default:
            break;
    }
    [self requestMoviePlayListWithTrade:trade
                                    tax:tax
                                  topic:topic
                              pageIndex:@(_lastId)
                                pageEnd:@(_lastId+9)
     ];
    [self removeMaskBlackView];

}

- (void)removeMaskBlackView {
    [self.maskBlackView removeFromSuperview];
    [self.tagListView removeFromSuperview];
    [self.arrowImgV removeFromSuperview];
    self.arrowImgV = nil;
    self.tagListView = nil;
    self.maskBlackView = nil;
    self.subCateListM = nil;
}

#pragma mark -- lazyLoad

-(SDCycleScrollView *)cycleScrollView {
    if (!_cycleScrollView) {
        //  顶部轮播器
        CGRect scrollViewFrame = CGRectMake(0,0, Main_Screen_Width, kScrollViewH);
        _cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:scrollViewFrame];
        _cycleScrollView.placeholderImage = [UIImage imageNamed:@"HomebackImage"];
        _cycleScrollView.infiniteLoop = YES;
        _cycleScrollView.autoScroll = YES;
        _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
        _cycleScrollView.hidesForSinglePage = YES;
        _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _cycleScrollView.autoScrollTimeInterval = 3.0;
        /** block方式监听点击 */
        _cycleScrollView.clickItemOperationBlock = [self scrollViewClickItemBlock];
        _cycleScrollView.currentPageDotColor = COLOR_NAVBAR_BLUE;
    }
    return _cycleScrollView;
}

-(UICollectionView *)featuresCollectionView {
    if (!_featuresCollectionView) {
        // 专题视图
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemMargin = (Main_Screen_Width - kCollectionViewItemW *4) /5.0;
        layout.itemSize = CGSizeMake(kCollectionViewItemW,kCollectionViewH);
        layout.sectionInset = UIEdgeInsetsMake(0, itemMargin, 0, 0);
        layout.minimumLineSpacing = itemMargin;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
//        CGFloat collectionViewY = CGRectGetMaxY(scrollViewFrame);
        CGFloat collectionViewY = kScrollViewH;
        CGRect collectionViewFrame = CGRectMake(0, collectionViewY, Main_Screen_Width, kCollectionViewH);
        
        _featuresCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
        _featuresCollectionView.backgroundColor = [UIColor whiteColor];
        _featuresCollectionView.scrollEnabled = NO;
        _featuresCollectionView.dataSource = self;
        _featuresCollectionView.delegate = self;
        [_featuresCollectionView registerClass:[BIZFeatureCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuseID];
        
        _featuresCollectionView.tag = kFeatureCollectionViewTag;

    }
    return _featuresCollectionView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        // 底部 tableView
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.estimatedRowHeight = 215;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        [_tableView registerClass:[BIZFeaturePlayerTableViewCell class] forCellReuseIdentifier:kTableViewCellReuseID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"BIZNoConsultorCell" bundle:nil] forCellReuseIdentifier:kNoDataCellReuseID];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewest)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadHistory)];
        
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource    = self;
        _tableView.delegate      = self;
    }
    return _tableView;
}

- (void)loadNewest {
    _lastId = 1;
    _sitcomListM = nil;
    NSString *trade     = @"";
    NSString *tax       = @"";
    NSString *topic     = @"";
    switch (_filterCateModel.pid.intValue) {
        case 1:
            trade   = _filterCateModel.uuid;
            break;
        case 2:
            tax     = _filterCateModel.uuid;
            break;
        case 3:
            topic   = _filterCateModel.uuid;
            break;
        default:
            break;
    }
    [self requestMoviePlayListWithTrade:trade
                                    tax:tax
                                  topic:topic
                              pageIndex:@(_lastId)
                                pageEnd:@(_lastId+9)
     ];

}

- (void)loadHistory {
    
    NSString *trade     = @"";
    NSString *tax       = @"";
    NSString *topic     = @"";
    switch (_filterCateModel.pid.intValue) {
        case 1:
            trade   = _filterCateModel.uuid;
            break;
        case 2:
            tax     = _filterCateModel.uuid;
            break;
        case 3:
            topic   = _filterCateModel.uuid;
            break;
        default:
            break;
    }
    [self requestMoviePlayListWithTrade:trade
                                    tax:tax
                                  topic:topic
                              pageIndex:@(_lastId)
                                pageEnd:@(_lastId+9)
     ];
   
}

- (UIView *)maskBlackView {
    if (!_maskBlackView) {
        // 蒙板
        _maskBlackView = [[UIView alloc] initWithFrame:self.view.bounds];
        _maskBlackView.backgroundColor = [UIColor blackColor];
        _maskBlackView.alpha = 0.5;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeMaskBlackView)];
        [_maskBlackView addGestureRecognizer:tap];
        
    }
    return _maskBlackView;
}

- (UIImageView *)arrowImgV {
    if (!_arrowImgV) {
        CGFloat arrowY = CGRectGetMaxY(self.featuresCollectionView.frame);
        _arrowImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, arrowY, Main_Screen_Width, kTagMargin)];
    }
    return _arrowImgV;
}

- (UICollectionView *)tagListView {
    if (!_tagListView) {
        // 标签列表
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kTagItemW,kTagItemH);
        layout.sectionInset = UIEdgeInsetsMake(0, kTagMargin, kTagMargin, kTagMargin);
        // 行间距
        layout.minimumLineSpacing = kTagMargin;
        //        layout.minimumInteritemSpacing = kTagMargin;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        
        CGFloat tagViewY = CGRectGetMaxY(self.arrowImgV.frame);
        _tagListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, tagViewY, Main_Screen_Width,kTagListViewH) collectionViewLayout:layout];
        _tagListView.tag = kTagListCollectionViewTag;
        _tagListView.backgroundColor = [UIColor whiteColor];
        _tagListView.scrollEnabled = YES;
        _tagListView.delegate = self;
        _tagListView.dataSource = self;
        
//        [_tagListView registerNib:[UINib nibWithNibName:@"BIZTagListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kTagListCellReuseID];
        [_tagListView registerClass:[BIZTagListCollectionViewCell class] forCellWithReuseIdentifier:kTagListCellReuseID];

    }
    return _tagListView;
}

- (void)stopRefresh
{
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
